import {createService} from '@utils/request'
import BASE_URL,{tokenKey} from '../javaBaseUrl'
// const BASE_URL = {
//     development: 'http://192.168.78.49:9019/cqp-project_manage_system/',
//     test: 'http://192.168.78.49:9019/cqp-project_manage_system/',
//     test2: 'http://192.168.78.49:9019/cqp-project_manage_system/',
//     production: 'http://192.168.78.49:9019/cqp-project_manage_system/'
// }
const request = createService(BASE_URL,tokenKey)
//获取菜单列表 懒加载
export const getMenus = (data) => {
    return request.get(`/permission/menus/lazy?pid=${data}`).then(res=>{
        res.map(item=>{
            item.hasChildren = !item.leaf
        })
        return res
    })
}
function getQuery(data){
    const keys = Object.keys(data)
    let str = '?'
    if(!keys.length) return ''
       for(let i=0;i<keys.length;i++){
           str = i===0 ? str+`${keys[i]}=${data[keys[i]]}`:str+`&${keys[i]}=${data[keys[i]]}`
       }
       return str
}
// 获取菜单列表 条件搜索
export const getMenusBySearch = (data) => {
    let params = ''
    if(data){
        params = getQuery(data)
    }
    return request.get(`/permission/menus${params}`).then(res=>{
        res.map(item=>{
            item.hasChildren = !item.leaf
        })
        return res
    })
}
//修改菜单
export const updateMenu = (data) => {
    return request.put(`/permission/menus`, data)
}
//新增菜单
export const addMenu = (data) => {
    return request.post(`/permission/menus`, data)
}
//删除菜单
export const deleteMenu = (data) => {
    return request.delete(`/permission/menus`, {data:data})
}

export default { getMenus, updateMenu, addMenu, deleteMenu, getMenusBySearch }